package com.imist.jdbc.servlet;

import com.imist.jdbc.bean.User;
import com.imist.jdbc.service.UserServices;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    private UserServices userServices;
    @Override
    public void init() throws ServletException {
        super.init();
        userServices = new UserServices();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = userServices.login(username,password);
        if (null != user){
            req.getSession().setAttribute("user",user);
            req.getRequestDispatcher("/message/list.do").forward(req,resp);
        }else {
            req.getRequestDispatcher("/login.do").forward(req,resp);
        }

    }

    @Override
    public void destroy() {
        super.destroy();

    }
}
